<?php

/**
 * Created by PhpStorm.
 * User: 子不语
 * Date: 2016/7/29
 * Time: 14:58
 */
class PackAction extends BaseAction
{
    function index(){
        $num=Req::get('num')?:0;
        if($num){
            $status=1;
            /*$sql='select count(id) as count from pack_list where uid='.$_SESSION['usersid'].' and time>'.strtotime(date('Y-m-d')).' and time<'.strtotime(date('Y-m-d 23:59:59'));
            $count=db::get_one($sql);
            $count->count=1;
            if($count->count>=3){
                $mess='对不起,每天只能领3次,您已超过限额';
            }else {*/
            $sql = 'set autocommit=0 ';
            db::_query($sql);
            db::_query('begin');
            $sql = 'SELECT p.*,pl.id as pid FROM pack p LEFT JOIN pack_list pl ON pl.`typeid`=p.id and pl.uid=' . $_SESSION['usersid'] . ' WHERE p.num=' . $num;
            $pack = db::get_one($sql);
            if (!$pack->id || $pack->status == 0) {
                $mess = '不存在此红包';
            } elseif ($pack->endcount >= $pack->count) {
                $mess = '您来晚了,红包已经抢完了';
            } elseif ($pack->pid) {
                $mess = '已经领取过了';
            } else {

                $ledou = rand($pack->min, $pack->max);
                $sql = 'update pack set endcount=endcount+1 where id=' . $pack->id . ' and count>endcount';
                if (!db::_query($sql, false)) {
                    db::_query('rollback');
                    $mess = '您来晚了,红包已经抢完了_';
                } else {
                    db::_query('commit');
                    db::_query('set autocommit=1');
                    $ip = get_ip();
                    $sql = 'insert into pack_list (typeid,uid,ledou,time,ip)values(' . $pack->id . ',' . $_SESSION['usersid'] . ',' . $ledou . ',' . time() . ',INET_ATON(\'' . $ip . '\'))';
                    if (!db::_query($sql)) {
                        db::_query('rollback');
                        $mess = '您来晚了,红包已经抢完了 0';
                    } else {

                        if (!db::_query('update users set points=points+' . $ledou . ' where id=' . $_SESSION['usersid'])) {
                            db::_query('rollback');
                            $mess = '您来晚了,红包已经抢完了 0';
                        } else {
//-- 更新中央银行
                            $this->update_centerbank($ledou, 9);
                            $sql = "INSERT game_static (uid,typeid,points) values (" . $_SESSION['usersid'] . ",141, $ledou) ON DUPLICATE KEY UPDATE points=points+'$ledou'";
                            db::_query($sql);
                            $sql = "INSERT webtj (time,pack) values (now(),$ledou) ON DUPLICATE KEY UPDATE pack=pack+'$ledou'";
                            db::_query($sql);

                            $status = 0;
                            $this->RefreshPoints();
                            $this->score_log($_SESSION['usersid'], 40, $ledou, $_SESSION['points'], $_SESSION['bankpoints'], '红包id=' . $pack->id);
                            $mess = '恭喜您,获得宝币: ' . $ledou;
                        }
                    }

                }
                //}

            }

            return $this->result($status,$mess );
        }
        $sql='select * from pack_list where uid='.$_SESSION['usersid'].'  order by id desc limit 30 ';
        $list=db::get_all($sql);
        $this->assign('list',$list);
        $this->display('pack_get');
    }
    function score_log($uid,$type,$amount,$points,$bankpoints,$remark=''){
        $data=array('uid'=>$uid,'opr_type'=>$type,//类型，0：存，1：取，2：充值体验卡，3：转账入，4：转账出,5:在线充值,6:领取救济,7:兑奖点卡,8:推荐收益\',
            'amount'=>$amount,
            'log_time'=>date('Y-m-d H:i:s'),
            'ip'=>get_ip(),
            'points'=>$points,
            'bankpoints'=>$bankpoints,
            'remark'=>$remark);
        db::_insert('score_log',$data );
    }
}